package com.lx.zxs.controller;

import com.lx.zxs.entity.User;
import com.lx.zxs.service.CostumerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@CrossOrigin
@RestController
@RequestMapping("coustumer")
public class CostumerController {

    @Autowired
    CostumerService costumerService;

    /**
     * 添加用户
     * @param username
     * @param phone
     * @param brithdate
     * @param collage
     * @param password
     * @param major
     * @return
     */
    @RequestMapping("insertUser")
    public int insertUser(String username, String phone, String brithdate, String collage, String password, String major) {

        User user = new User();
        user.setUsername(username);
        user.setPhone(phone);
        user.setPassword(password);
        SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd");
        try {
            //使用SimpleDateFormat的parse()方法生成Date
            Date date = sf.parse(brithdate);
            user.setBirthdate(date.toString());
        } catch (ParseException e) {
            e.printStackTrace();
        }
        user.setCollage(collage);
        user.setMajor(major);

        return costumerService.insertUser(user);

    }

    /**
     * 修改用户
     *
     * @param username
     * @param phone
     * @param brithdate
     * @param password
     * @param major
     * @return
     */
    @RequestMapping("updateUser")
    public int updateUser(int id,String username, String phone, String brithdate, String collage, String password, String major) {
        User user = new User();
        user.setUsername(username);
        user.setPhone(phone);
        user.setPassword(password);
        SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd");
        try {
            //使用SimpleDateFormat的parse()方法生成Date
            Date date = sf.parse(brithdate);
            user.setBirthdate(date.toString());
        } catch (ParseException e) {
            e.printStackTrace();
        }
        user.setCollage(collage);
        user.setMajor(major);
        user.setId(id);
        return costumerService.updateUser(user);
    }

    /**
     * 删除用户
     *
     * @param id
     * @return
     */
    @RequestMapping("delUser")
    public int delUser(int id) {

        return costumerService.delUser(id);
    }

    /**
     * 查询用户
     * @param keyWords
     * @param id
     * @return
     */
    @RequestMapping("queryUserByParms")
    public List<User> queryUserByParms(String keyWords, Integer id) {

        return costumerService.queryUserByParms(keyWords, id);

    }

}
